#include <iostream>
#include <future>
#include <functional>
#include <unistd.h>
#include <syscall.h>

volatile bool g_bExit = false;

#ifndef gettid
#define gettid() syscall(SYS_gettid)
#endif

int32_t AsyncTask()
{
    while (!g_bExit)
    {
        std::cout << "[" << getpid() << "]["<< gettid() << "] Async task" << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }

    return 0;
}

int main(int argc, char** argv)
{
    std::cout << "[" << getpid() << "]["<< gettid() << "] Enter main task" << std::endl;

    std::future<int32_t> result = std::async(std::launch::async, std::bind(AsyncTask));

    try
    {
        std::cout << "result: " << result.get() << std::endl;
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what() << '\n';
    }

    std::cout << "[" << getpid() << "]["<< gettid() << "] Exit main task" << std::endl;

    return 0;
}